﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace TRAC_Demo
{
    // T.R.A.C.
    // Main Author(s): Morgan Davis
    // Additional Authors:
    // Date: 10/20/2013
    // This is a replica of the generate ticket form that handles the viewing of tickets from the resolved ticket table

    public partial class FormResolvedTicketOverview : Form
    {
        #region [ Fields ]

        TRACobjectsDataContext ti = new TRACobjectsDataContext();
        int tickVal;

        #endregion

        #region [ Constructors ]

        public FormResolvedTicketOverview(int passedValue)
        {
            InitializeComponent();
            this.BackColor = CurrentUser.UserColor;
            this.tickVal = passedValue;
        }

        #endregion

        #region [ Events ]

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormTicketOverview_Load(object sender, EventArgs e)
        {
            radNoDel.Checked = true;
            btnDelete.Enabled = false;

            // Opens the ticket selected on the general overview form
            var tickQuery = from t in ti.ResolvedTickets where t.TicketNumber == tickVal select t;

            foreach (var thisTicket in tickQuery)
            {
                txtTicketNumber.Text = thisTicket.TicketNumber.ToString();
                txtAgentNumber.Text = thisTicket.AssignedAgent.ToString();
                dtpDate.Value = thisTicket.DateCreated;
                txtEmployee.Text = thisTicket.EmployeeID.ToString();
                txtIssue.Text = thisTicket.SitCode.ToString();
                comboPriority.Text = thisTicket.PriorityCode.ToString();
                txtNotes.Text = thisTicket.NoteField;
                comboStatus.Text = thisTicket.StatusCode;
                lblCreated.Text = thisTicket.CreatingAgent.ToString();
            }

            // Gets employee information using their corresponding number and displays it on the form
            var empName = from en in ti.Employees where en.EmployeeID == int.Parse(txtEmployee.Text) select en;

            foreach (var thisName in empName)
            {
                txtEmployeeFirst.Text = thisName.fName.ToString();
                txtEmployeeLast.Text = thisName.lName.ToString();
            }

            // Gets agent information using their corresponding number and displays it on the form
            var agentName = from ag in ti.Employees where ag.EmployeeID == int.Parse(txtAgentNumber.Text) select ag;

            foreach (var thisName in agentName)
            {
                txtAgentFirst.Text = thisName.fName.ToString();
                txtAgentLast.Text = thisName.lName.ToString();
            }
        }

        // Disables the delete button when the disable delete radio button is checked
        private void radNoDel_CheckedChanged(object sender, EventArgs e)
        {
            btnDelete.Enabled = false;
        }

        // Enables the delete button when the enable delete radio button is checked
        private void radDel_CheckedChanged(object sender, EventArgs e)
        {
            btnDelete.Enabled = true;
        }

        // Deletes the ticket on user confirmation
        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult myResult = MessageBox.Show("Are you sure you want to permanently delete this ticket?", "Verify Ticket Deletion", MessageBoxButtons.YesNo);
            if (myResult == DialogResult.Yes)
            {
                var deleteTicket = from tic in ti.ResolvedTickets where tic.TicketNumber == tickVal select tic;
                foreach (var getRid in deleteTicket)
                {
                    ti.ResolvedTickets.DeleteOnSubmit(getRid);
                }
                ti.SubmitChanges();
                ti.Dispose();
                this.Close();
            }
            else
            {
                MessageBox.Show("Ticket Deletion Cancelled");
                radNoDel.PerformClick();
            }
        }

        // Submits the ticket for update
        // Catches possible exceptions
        private void btnSubmit_Click(object sender, EventArgs e)
        {        
                try
                {
                    UpdateTicketResolved();
                }
                catch (SqlException)
                {
                    MessageBox.Show("Something has gone wrong, more than likely you have entered an invalid Employee or Agent ID number.", "Whoa, Whoa there BUDDY!!");
                }   
        }

        // Prints the current form
        private void btnPrint_Click(object sender, EventArgs e)
        {
            PrintDocument pd = new PrintDocument();
            pd.PrintPage += new PrintPageEventHandler(PrintImage);
            pd.Print();
        }

        // Disables the update button when the disable update radio button is checked
        private void radEnableUpdate_CheckedChanged(object sender, EventArgs e)
        {
            btnUpdate.Enabled = true;
        }

        // Enables the update button when the enable update radio button is checked
        private void radDisableUpdate_CheckedChanged(object sender, EventArgs e)
        {
            btnUpdate.Enabled = false;
        }

        #endregion

        #region [ Methods ]

        // Updates the ticket with user applied changes

        private void UpdateTicketResolved()
        {
            //add ticket to resolved ticket table
            TRACobjectsDataContext rti = new TRACobjectsDataContext();
            var updateQuery = from t in rti.ResolvedTickets where t.TicketNumber == tickVal select t;

            foreach (var update in updateQuery)
            {   
                update.NoteField = txtNotes.Text;  
            }

            rti.SubmitChanges();
            this.Close();
            rti.Dispose();
        }

        // Prints the form
        void PrintImage(object o, PrintPageEventArgs e)
        {
            int x = SystemInformation.WorkingArea.X;
            int y = SystemInformation.WorkingArea.Y;
            int width = this.Width;
            int height = this.Height;

            Rectangle bounds = new Rectangle(x, y, width, height);

            Bitmap img = new Bitmap(width, height);

            this.DrawToBitmap(img, bounds);
            Point p = new Point(100, 100);
            e.Graphics.DrawImage(img, p);
        }

        #endregion
    }
}
